<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfFundamentals.Controls"
    xmlns:converter="clr-namespace:WpfFundamentals.Converter"
    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfFundamentals;component/Themes/Button.Generic.xaml" />
    </ResourceDictionary.MergedDictionaries>
    
    <LinearGradientBrush StartPoint="0,0.5"
                         EndPoint="1,0.5"
                         x:Key="titleBackgroundBrush">
        <GradientStop Color="#F0F0F0" />
        <GradientStop Color="Transparent" Offset="1.0" />
    </LinearGradientBrush>

    <LinearGradientBrush StartPoint="0,0.5"
                         EndPoint="1,0.5"
                         x:Key="titleBorderBrush">
        <GradientStop Color="#A0A0A0" Offset="1.0" />
        <GradientStop Color="#D0D0D0" Offset="0.0" />
    </LinearGradientBrush>

    <SolidColorBrush x:Key="WindowBackgroundBrush"
                     Color="#FFF" />

    <SolidColorBrush x:Key="HeaderBorderBrush"
                     Color="Silver" />
    

    <converter:BoolConverter x:Key="VisibilityConverter"
                                FalseValue="Collapsed"
                                TrueValue="Visible" />

    <ControlTemplate x:Key="PopupButtonTemplate"
                     TargetType="{x:Type local:PopupButton}">
        <Grid Name="Normal"
              HorizontalAlignment="{TemplateBinding Button.HorizontalAlignment}"
              VerticalAlignment="{TemplateBinding Button.VerticalAlignment}">
            <ToggleButton Name="PART_Toggle"
                          Background="Transparent"
                          Command="{TemplateBinding Command}"
                          IsTabStop="{TemplateBinding IsTabStop}"
                          IsHitTestVisible="{TemplateBinding IsHitTestVisible}"
                          CommandParameter="{TemplateBinding CommandParameter}"
                          Style="{StaticResource MouseOverToggleButtonStyle}">
                <ContentPresenter RecognizesAccessKey="False" />
            </ToggleButton>

            <Popup Name="xPopup"
                   Width="{TemplateBinding PopupWidth}"
                   Height="{TemplateBinding PopupHeight}"
                   AllowsTransparency="True"
                   HorizontalOffset="{TemplateBinding HorizontalOffset}"
                   IsOpen="{Binding Path=IsChecked,
                                    ElementName=PART_Toggle}"
                   Placement="{TemplateBinding PopupPlacement}"
                   PlacementTarget="{Binding ElementName=PART_Toggle}"
                   StaysOpen="False"
                   VerticalOffset="{TemplateBinding VerticalOffset}">
                <Border Background="{StaticResource WindowBackgroundBrush}"
                        BorderBrush="{StaticResource HeaderBorderBrush}"
                        BorderThickness="2.0"
                        CornerRadius="4"
                        Padding="3">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="20" />
                            </Grid.ColumnDefinitions>
                            <Border Name="xHeader"
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    Margin="0,0,5,0"
                                    Background="{StaticResource titleBackgroundBrush}"
                                    BorderBrush="{StaticResource titleBorderBrush}"
                                    BorderThickness="1,0,1,1"
                                    CornerRadius="4,4,0,0"
                                    Padding="2"
                                    TextBlock.FontWeight="Bold">
                                <TextBlock Foreground="Black"
                                           Text="{TemplateBinding PopupHeader}" />
                            </Border>
                            <Button Name="PART_CloseButton"
                                    Grid.Column="1"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Style="{StaticResource MouseOverButtonStyle}"         
                                    Visibility="{TemplateBinding HasPopupHeader, Converter={StaticResource VisibilityConverter}}">
                                <Button.Content>
                                    <Image Source="pack://application:,,,/WpfFundamentals;component/Themes/cancel.png" Height="16" Width="16" />
                                </Button.Content>
                            </Button>
                        </Grid>
                        <ContentControl Grid.Row="1"
                                        Content="{TemplateBinding PopupContent}" />
                    </Grid>
                </Border>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="PopupHeader" Value="{x:Null}">
                <Setter TargetName="xHeader" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="PopupHeader" Value="">
                <Setter TargetName="xHeader" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="PopupWidth" Value="0">
                <Setter TargetName="xPopup" Property="Width" Value="Auto" />
            </Trigger>
            <Trigger Property="PopupHeight" Value="0">
                <Setter TargetName="xPopup" Property="Height" Value="Auto" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="PopupButtonStyle"
           TargetType="{x:Type local:PopupButton}">
        <Setter Property="Template" Value="{StaticResource PopupButtonTemplate}" />
    </Style>

    <Style TargetType="{x:Type local:PopupButton}">
        <Setter Property="Template" Value="{StaticResource PopupButtonTemplate}" />
    </Style>


</ResourceDictionary>
